﻿using System;
using System.Drawing;
using System.Linq;
using ArtMath.Resource;
using ArtMath.PointCollection;
using ArtMath.Core.Data;
using ArtMath.Attribute;
using ArtMath.Helper;
using ArtMath.Core.Helper;
using ArtMath.CommonType;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 三点扇形
    /// </summary>
    [Serializable]
    public class ThreePointsSector : ThreePointsPartialCircleBase
    {
        public ThreePointsSector(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.sdsx, addToList)
        {
            pske.Curves = new Curve[] { new Curve() };
        }
        public override PointCollectionsManager Skeleton
        {
            get
            {
                PointCollectionsManager res = base.Skeleton;
                LineSegment ls1 = new LineSegment(Center, Point1.ToPointD()),
                    ls2 = new LineSegment(Point2.ToPointD(), Center);
                res.AddPointCollection(ls1);
                res.AddPointCollection(ls2);
                res.ConnectHeadAndTail(ls1, ske);
                res.ConnectHeadAndTail(ske, ls2);
                res.ConnectHeadAndTail(ls2, ls1);
                return res;
            }
        }
        public override PointCollectionsManager PSkeleton
        {
            get
            {
                PointCollectionsManager res = base.PSkeleton;
                LineSegment ls1 = new LineSegment(PCenter, Point1.Pxy),
                    ls2 = new LineSegment(Point2.Pxy, PCenter);
                res.AddPointCollection(ls1);
                res.AddPointCollection(ls2);
                res.ConnectHeadAndTail(ls1, pske);
                res.ConnectHeadAndTail(pske, ls2);
                res.ConnectHeadAndTail(ls2, ls1);
                return res;
            }
        }
        public override PointCollectionsManager NewSkeleton
        {
            get
            {
                PointCollectionsManager res = base.NewSkeleton;
                PointCollectionBase tmp = res.PointCollections.Single();
                LineSegment ls1 = new LineSegment(NewCenter, Point1.NewXY),
                    ls2 = new LineSegment(Point2.NewXY, NewCenter);
                res.AddPointCollection(ls1);
                res.AddPointCollection(ls2);
                res.ConnectHeadAndTail(ls1, tmp);
                res.ConnectHeadAndTail(tmp, ls2);
                res.ConnectHeadAndTail(ls2, ls1);
                return res;
            }
        }
        public override PointCollectionsManager NewPSkeleton
        {
            get
            {
                PointCollectionsManager res = base.NewPSkeleton;
                PointCollectionBase tmp = res.PointCollections.Single();
                LineSegment ls1 = new LineSegment(NewPCenter, Point1.NewPxy),
                    ls2 = new LineSegment(Point2.NewPxy, NewPCenter);
                res.AddPointCollection(ls1);
                res.AddPointCollection(ls2);
                res.ConnectHeadAndTail(ls1, tmp);
                res.ConnectHeadAndTail(tmp, ls2);
                res.ConnectHeadAndTail(ls2, ls1);
                return res;
            }
        }
        [MultilingualDisplayName("SectorCenter")]
        public override PointD Center
        {
            get
            {
                return base.Center;
            }
        }
        [MultilingualDisplayName(null, "SectorCenter", "Displayer")]
        public override PropertyDisplayer CenterDisplayer
        {
            get
            {
                return base.CenterDisplayer;
            }

            set
            {
                base.CenterDisplayer = value;
            }
        }
        protected override void ReCalcParams(bool fromBuilding)
        {
            Circumference += 2 * Radius;
            if (fromBuilding)
            {
                ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{Resources.Translate("ArcLength")} = {ArcLength}{Resources.CommaSeparate}{Resources.Translate("ChordLength")} = {ChordLength}{Resources.CommaSeparate}{Resources.Translate("SectorCenter")}{Resources.ColonSeparate}{Pad.ShowPoint(Center)}{Resources.CommaSeparate}{Resources.Translate("Radius")} = {Radius}{Resources.CommaSeparate}{Resources.Translate("CentralAngle")} = {CentralAngle}");
            }
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible && IsCreationCompleted)
            {
                Brush bru = FillBrush;
                if (bru != null) gra.DrawSector(LinePen, bru, PCenter, Pad.Coordinate == null ? 0 : -Pad.Coordinate.RotateAngle.DegreeValue, pske.LongAxisHalfLength, pske.ShortAxisHalfLength, PStart * Core.Helper.Angle.RToD, PSweep * Core.Helper.Angle.RToD);
            }
            base.Paint(gra);
        }
        public override void PaintVirtualObjects(Graphics gra)
        {
            if (CanDrawVirtual)
            {
                gra.DrawSector(PCenter, Pad.Coordinate == null ? 0 : -Pad.Coordinate.RotateAngle.DegreeValue, pske.LongAxisHalfLength, pske.ShortAxisHalfLength, PStart * Core.Helper.Angle.RToD, PSweep * Core.Helper.Angle.RToD);
            }
            base.PaintVirtualObjects(gra);
        }
        public override void PaintTrack(Graphics gra)
        {
            if (ShowTrack_)
            {
                for (int i = 0; i < track.Count; i++)
                {
                    gra.DrawSector(TrackPen, null, track[i].Key, track[i].Value[0], track[i].Value[1], track[i].Value[2], track[i].Value[3], track[i].Value[4]);
                }
            }
            base.PaintTrack(gra);
        }
    }
}
